package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.CompanyShowDao;
import com.yenlien.traceyun.business.entity.CompanyShow;
import com.yenlien.traceyun.business.service.rest.usercenter.CompanyRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-21
 * @description:
 **/
@Api(tags = "企业信息")
@RestController
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyRestService companyRestService;

    @Autowired
    private CompanyShowDao companyShowDao;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject jsonObject) {
        return companyRestService.findByList(jsonObject);
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return companyRestService.findById(id);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        JSONObject result = (JSONObject) companyRestService.save(map, files);
        JSONObject commpany = result.getJSONObject("data");
        CompanyShow show = new CompanyShow();
        show.setCompanyId(commpany.getString("id"));
        show.setCode(show.getCompanyId());
        companyShowDao.save(show);
        return result;

    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody JSONObject jsonObject) {
        JSONObject result = (JSONObject) companyRestService.save2(jsonObject);
        JSONObject commpany = result.getJSONObject("data");
        CompanyShow show = new CompanyShow();
        show.setCompanyId(commpany.getString("id"));
        show.setCode(show.getCompanyId());
        companyShowDao.save(show);
        return result;

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return companyRestService.update(map, files);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        return companyRestService.delete(id);
    }

}
